<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="通行证编号" prop="passCode">
        <el-input
          v-model="queryParams.passCode"
          placeholder="请输入通行证编号"
          style="width: 240px"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通行证状态" prop="passStatus">
        <el-select v-model="queryParams.passStatus"
                   style="width: 240px"
                   placeholder="请选择通行证状态" clearable>
          <el-option
            v-for="dict in flow_pass_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['flow:flowPass:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['flow:flowPass:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['flow:flowPass:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['flow:flowPass:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flowPassList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="passId" />-->
      <el-table-column label="序号" align="center" width="50" type="index" />
      <el-table-column label="流动申请编码" align="center" width="250" prop="applyId" >
        <template #default="scope">
          {{ (flowApplyList.find(item => item.applyId === scope.row.applyId) || {}).applyCode || '未找到关联的申请单信息' }}
        </template>
      </el-table-column>
      <el-table-column label="通行证编号" align="center" width="250" prop="passCode" >
        <template #default="scope">
          <el-link type="primary" @click="getFlowPassDetail(scope.row)" v-hasPermi="['population:hukou:list']">
            {{ scope.row.passCode }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="通行证状态" align="center" width="200px" prop="passStatus">
        <template #default="scope">
          <dict-tag :options="flow_pass_status" :value="scope.row.passStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="通行证生效时间" align="center" wi prop="passStartTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.passStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="通行证失效时间" align="center" prop="passEndTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.passEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="操作" align="center" width="200px"
                       fixed="right" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['flow:flowPass:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['flow:flowPass:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改通行证概览对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="flowPassRef" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="通行证状态" prop="passStatus">
          <el-select v-model="form.passStatus" placeholder="请选择通行证状态">
            <el-option
              v-for="dict in flow_pass_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 详情 -->
    <el-dialog title="通行证信息详情" v-model="flowPassDetailOpen" width="800px" append-to-body>
      <el-descriptions border :column="1">
        <el-descriptions-item label="流动申请编码">
          {{ (flowApplyList.find(item => item.applyId === form.applyId) || {}).applyCode || '未找到关联的申请单信息' }}
        </el-descriptions-item>
        <el-descriptions-item label="申请人姓名">{{ form.population.populationName }}</el-descriptions-item>
        <el-descriptions-item label="申请人电话">{{ form.population.populationPhone }}</el-descriptions-item>
        <el-descriptions-item label="照片">
          <image-preview :src="form.population.populationImage" :width="50" :height="50"/>
        </el-descriptions-item>
        <el-descriptions-item label="身份证明附件">
          <el-button type="primary" :disabled="!(form.population.populationIdCardFile)" @click="download.resource(form.population.populationIdCardFile)">
            下载
          </el-button>
        </el-descriptions-item>
        <el-descriptions-item label="通行证编号">{{ form.passCode }}</el-descriptions-item>
        <el-descriptions-item label="通行证编号">{{ form.passCode }}</el-descriptions-item>
        <el-descriptions-item label="通行证状态">
          <dict-tag :options="flow_pass_status" :value="form.passStatus"/>
        </el-descriptions-item>
        <el-descriptions-item label="通行证生效时间">{{ form.passStartTime }}</el-descriptions-item>
        <el-descriptions-item label="通行证失效时间">{{ form.passEndTime }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ form.createTime }}</el-descriptions-item>
        <el-descriptions-item label="创建人">{{ form.createBy }}</el-descriptions-item>
        <el-descriptions-item label="修改时间">{{ form.updateTime }}</el-descriptions-item>
        <el-descriptions-item label="修改人">{{ form.updateBy }}</el-descriptions-item>
        <el-descriptions-item label="备注" v-if="form.remark && form.remark.trim() !== ''" width="100">
          <el-input v-model="form.remark" type="textarea" :readOnly="true" disabled />
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>

  </div>
</template>

<script setup name="FlowPass">
import { listFlowPass, getFlowPass, delFlowPass, addFlowPass, updateFlowPass } from "@/api/flow/flowPass";
import {listFlowApply} from "@/api/flow/flowApply.js";
import {loadAllParams} from "@/api/page.js";
import download from "@/plugins/download.js";

const { proxy } = getCurrentInstance();
const { flow_pass_status } = proxy.useDict('flow_pass_status');

const flowPassList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    passCode: null,
    passStatus: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

// 自定义区域
const flowApplyList = ref([]);
// 控制详情弹窗
const flowPassDetailOpen = ref(false);

/** 查询通行证概览列表 */
function getList() {
  loading.value = true;
  listFlowPass(queryParams.value).then(response => {
    flowPassList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    passId: null,
    applyId: null,
    passCode: null,
    passStatus: null,
    passStartTime: null,
    passEndTime: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
    delFlag: null,
    population: null
  };
  proxy.resetForm("flowPassRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.passId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加通行证";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _passId = row.passId || ids.value
  getFlowPass(_passId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改通行证";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["flowPassRef"].validate(valid => {
    if (valid) {
      if (form.value.passId != null) {
        updateFlowPass(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFlowPass(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _passIds = row.passId || ids.value;
  proxy.$modal.confirm('是否确认删除通行证概览编号为"' + _passIds + '"的数据项？').then(function() {
    return delFlowPass(_passIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('flow/flowPass/export', {
    ...queryParams.value
  }, `flowPass_${new Date().getTime()}.xlsx`)
}

getList();

// 自定义区域
/** 查询流动申请列表 */
function getFlowApplyList() {
  listFlowApply(loadAllParams).then(response => {
    flowApplyList.value = response.rows;
  });
}
getFlowApplyList()

// 获取通行证详情
function getFlowPassDetail(row) {
  reset();
  const _id = row.passId;
  getFlowPass(_id).then(response => {
    form.value = response.data;
    flowPassDetailOpen.value = true;
  })
}
</script>
